package com.jn.exam.question.service.impl;

import com.jn.exam.HTMLUtil;
import com.jn.exam.question.entity.PaperQuestion;
import com.jn.exam.question.entity.PersonalQuestion;
import com.jn.exam.question.entity.Question;
import com.jn.exam.question.mapper.PaperQuestionMapper;
import com.jn.exam.question.mapper.PersonalQuestionMapper;
import com.jn.exam.question.mapper.QuestionMapper;
import com.jn.exam.question.service.OssFeignService;
import com.jn.exam.question.service.PaperQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 江南大学1033190417
 * @since 2022-02-06
 */
@Service
public class PaperQuestionServiceImpl extends ServiceImpl<PaperQuestionMapper, PaperQuestion> implements PaperQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private PaperQuestionMapper paperQuestionMapper;

    @Autowired
    private OssFeignService ossFeignService;

    @Autowired
    private PersonalQuestionMapper personalQuestionMapper;


    @Override
    public boolean exchangeLocationById(Map<String, Object> param) {
        return baseMapper.exchangeLocationById(param);
    }



    @Async
    @Override
    public Future<Boolean> addQuestionToPaper(String paperId, String questionId, Float location, Float score) {
        Question question = questionMapper.selectById(questionId);
        PaperQuestion paperQuestion = new PaperQuestion();
        String name = question.getName();
        String answer = question.getAnswer();
        List<String> nameUrl = HTMLUtil.getUrl(name);
        List<String> answerUrl = HTMLUtil.getUrl(answer);
        if (!nameUrl.isEmpty()) {
            for (String url : nameUrl) {
                String u = ossFeignService.copyFileByUrl(url);
                name = name.replaceAll(url, u);
            }
        }
        if (!answerUrl.isEmpty()) {
            for (String url : answerUrl) {
                String u = ossFeignService.copyFileByUrl(url);
                answer = answer.replaceAll(url, u);
            }
        }
        paperQuestion.setName(name);
        paperQuestion.setAnswer(answer);
        paperQuestion.setScore(score);
        paperQuestion.setLocation(location);
        paperQuestion.setPaperId(paperId);
        paperQuestion.setType(question.getType());
        int insert = paperQuestionMapper.insert(paperQuestion);
        if (insert == 1) {
            return new AsyncResult<>(true);
        }
        return new AsyncResult<>(false);
    }

    @Async
    @Override
    public Future<Boolean> addPersonalQuestionToPaper(String paperId, String personalQuestionId, Float location, Float score) {
        PersonalQuestion personalQuestion = personalQuestionMapper.selectById(personalQuestionId);
        PaperQuestion paperQuestion = new PaperQuestion();
        String name = personalQuestion.getName();
        String answer = personalQuestion.getAnswer();
        List<String> nameUrl = HTMLUtil.getUrl(name);
        List<String> answerUrl = HTMLUtil.getUrl(answer);
        if (!nameUrl.isEmpty()) {
            for (String url : nameUrl) {
                String u = ossFeignService.copyFileByUrl(url);
                name = name.replaceAll(url, u);
            }
        }
        if (!answerUrl.isEmpty()) {
            for (String url : answerUrl) {
                String u = ossFeignService.copyFileByUrl(url);
                answer = answer.replaceAll(url, u);
            }
        }
        paperQuestion.setName(name);
        paperQuestion.setAnswer(answer);
        paperQuestion.setScore(score);
        paperQuestion.setLocation(location);
        paperQuestion.setPaperId(paperId);
        paperQuestion.setType(personalQuestion.getType());
        int insert = paperQuestionMapper.insert(paperQuestion);
        if (insert == 1) {
            return new AsyncResult<>(true);
        }
        return new AsyncResult<>(false);
    }

    @Override
    public List<PaperQuestion> takeOneExam(String paperId, String studentId) {
        Map<String, Object> param = new HashMap<>();
        param.put("paperId", paperId);
        param.put("studentId", studentId);
        return baseMapper.takeOneExam(param);
    }

    @Override
    public List<PaperQuestion> markingStudentPaper(String paperId, String studentId) {
        Map<String, Object> param = new HashMap<>();
        param.put("paperId", paperId);
        param.put("studentId", studentId);
        return baseMapper.markingStudentPaper(param);
    }


    @Override
    public Integer getExamStudentMarkStatus(String examId, String studentId) {
        Map<String, Object> param = new HashMap<>();
        param.put("examId", examId);
        param.put("studentId", studentId);
        return baseMapper.getExamStudentMarkStatus(param);
    }

    @Override
    public boolean updateExamStudentStatus(String examId, String studentId, Integer status) {
        Map<String, Object> param = new HashMap<>();
        param.put("examId", examId);
        param.put("studentId", studentId);
        param.put("status", status);
        return baseMapper.updateExamStudentStatus(param);
    }
}
